package com.ruoyi.gzmx_scdw.service.impl;


import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

import com.ruoyi.Gzmx_device.domain.Gzmx_device;
import com.ruoyi.Gzmx_device.service.IGzmx_deviceService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.ZtreeContans;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.gzmx_scdw.domain.Gzmx_scdwVo;
import com.ruoyi.gzmx_scdw.domain.ZtreeNew;
import com.ruoyi.gzmx_tag.domain.Gzmx_tag;
import com.ruoyi.youtian.domain.*;
import com.ruoyi.youtian.mapper.TSceneCategoryMapper;
import com.ruoyi.youtian.service.IPointEquipmentService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gzmx_scdw.mapper.Gzmx_scdwMapper;
import com.ruoyi.gzmx_scdw.domain.Gzmx_scdw;
import com.ruoyi.gzmx_scdw.service.IGzmx_scdwService;
import com.ruoyi.common.core.text.Convert;

/**
 * gzmx_scdwService业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-25
 */
@Service
public class Gzmx_scdwServiceImpl implements IGzmx_scdwService
{


    @Autowired
    private IGzmx_deviceService gzmxDeviceService;

    @Autowired
    private IPointEquipmentService pointEquipmentService;

    @Autowired
    private Gzmx_scdwMapper gzmx_scdwMapper;

    @Autowired
    private TSceneCategoryMapper sceneCategoryMapper;

    /**
     * 查询gzmx_scdw
     * 
     * @param id gzmx_scdw主键
     * @return gzmx_scdw
     */
    @Override
    public Gzmx_scdw selectGzmx_scdwById(Long id)
    {
        return gzmx_scdwMapper.selectGzmx_scdwById(id);
    }

    /**
     * 查询gzmx_scdw列表
     * 
     * @param gzmx_scdw gzmx_scdw
     * @return gzmx_scdw
     */
    @Override
    public List<Gzmx_scdw> selectGzmx_scdwList(Gzmx_scdw gzmx_scdw)
    {
        return gzmx_scdwMapper.selectGzmx_scdwList(gzmx_scdw);
    }

    /**
     * 新增gzmx_scdw
     * 
     * @param gzmx_scdw gzmx_scdw
     * @return 结果
     */
    @Override
    public int insertGzmx_scdw(Gzmx_scdw gzmx_scdw)
    {
        gzmx_scdw.setCreateTime(DateUtils.getNowDate());
        return gzmx_scdwMapper.insertGzmx_scdw(gzmx_scdw);
    }

    /**
     * 修改gzmx_scdw
     * 
     * @param gzmx_scdw gzmx_scdw
     * @return 结果
     */
    @Override
    public int updateGzmx_scdw(Gzmx_scdw gzmx_scdw)
    {
        gzmx_scdw.setUpdateTime(DateUtils.getNowDate());
        return gzmx_scdwMapper.updateGzmx_scdw(gzmx_scdw);
    }

    /**
     * 批量删除gzmx_scdw
     * 
     * @param ids 需要删除的gzmx_scdw主键
     * @return 结果
     */
    @Override
    public int deleteGzmx_scdwByIds(String ids)
    {
        return gzmx_scdwMapper.deleteGzmx_scdwByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除gzmx_scdw信息
     * 
     * @param id gzmx_scdw主键
     * @return 结果
     */
    @Override
    public int deleteGzmx_scdwById(Long id)
    {
        return gzmx_scdwMapper.deleteGzmx_scdwById(id);
    }

    /**
     * 查询gzmx_scdw树列表
     * 
     * @return 所有gzmx_scdw信息
     */
    @Override
    public List<Ztree> selectGzmx_scdwTree()
    {
        List<Gzmx_scdw> gzmx_scdwList = gzmx_scdwMapper.selectGzmx_scdwList(new Gzmx_scdw());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (Gzmx_scdw gzmx_scdw : gzmx_scdwList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(gzmx_scdw.getId());
            ztree.setpId(gzmx_scdw.getFdwdm());
            ztree.setName(gzmx_scdw.getDwmc());
            ztree.setTitle(gzmx_scdw.getDwmc());
            ztrees.add(ztree);
        }
        return ztrees;
    }

    @Override
    public List<Ztree> selectGzmx_scdwTreeNew()
    {
        List<Gzmx_scdw> gzmx_scdwList = gzmx_scdwMapper.selectGzmx_scdwList(new Gzmx_scdw());
        List<Gzmx_scdw> collect = gzmx_scdwList.stream().filter(scdw ->
                scdw.getJb() <= 3L
        ).collect(Collectors.toList());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (Gzmx_scdw gzmx_scdw : collect)
        {
            Ztree ztree = new Ztree();
            ztree.setId(gzmx_scdw.getId());
            ztree.setpId(gzmx_scdw.getFdwdm());
            ztree.setName(gzmx_scdw.getDwmc());
            ztree.setTitle(gzmx_scdw.getDwmc());
            ztrees.add(ztree);
        }
        return ztrees;
    }


    @Override
    public List<TreeSelect> selectGzmx_scdwTreeSelectList(Gzmx_scdw gzmxScdw) {
        List<TreeSelect> list = new ArrayList<TreeSelect>();
        List<Gzmx_scdwVo> resultList = new ArrayList<Gzmx_scdwVo>();
        List<Gzmx_scdw> gzmxScdwList = this.selectGzmx_scdwList(gzmxScdw);
        if(gzmxScdwList != null){
            List<Gzmx_scdwVo> gzmx_scdwVoList = new ArrayList<Gzmx_scdwVo>(gzmxScdwList.size());
            Gzmx_scdwVo vo = null;
            for (Gzmx_scdw bean : gzmxScdwList) {
                vo = new Gzmx_scdwVo();
                BeanUtils.copyProperties(bean, vo);
                gzmx_scdwVoList.add(vo);
            }
            for (Gzmx_scdwVo bean : gzmx_scdwVoList) {
                if(bean.getJb() == 1){
                    recursionFn(gzmx_scdwVoList, bean);
                    resultList.add(bean);
                }
            }

            List<TreeSelect> treeSelectScdwList = resultList.stream().map(TreeSelect::new).collect(Collectors.toList());
            // 生产点位
            //List<Gzmx_device> gzmx_deviceList = gzmxDeviceService.selectGzmx_deviceList(null);
            //            TreeSelect treeDevice = null;
//            List<TreeSelect> childTreeDeviceList = null;
//            List<TreeSelect> childTreeTagList = null;
//            List<PointEquipment> pointEquipmentList = pointEquipmentService.selectPointEquipmentList(null);
//            List<TreeSelect> resultTreeSelect = treeSelectScdwList.stream().peek(s -> {
//
//                recursionFnTree(s, gzmx_deviceList, pointEquipmentList);
//            }).collect(Collectors.toList());

            return treeSelectScdwList;
        }
        return list;
    }

    /**
     * 获得生产点位五级树结构
     * @param gzmxScdw
     * @return
     */
    @Override
    public List<TreeSelect> selectGzmx_scdwTreeSelectListNew(Gzmx_scdw gzmxScdw) {
        List<TreeSelect> list = new ArrayList<TreeSelect>();
        List<Gzmx_scdwVo> resultList = new ArrayList<Gzmx_scdwVo>();
        List<Gzmx_scdw> gzmxScdwList = this.selectGzmx_scdwList(gzmxScdw);
        if(gzmxScdwList != null){
            List<Gzmx_scdwVo> gzmx_scdwVoList = new ArrayList<Gzmx_scdwVo>(gzmxScdwList.size());
            Gzmx_scdwVo vo = null;
            for (Gzmx_scdw bean : gzmxScdwList) {
                vo = new Gzmx_scdwVo();
                BeanUtils.copyProperties(bean, vo);
                gzmx_scdwVoList.add(vo);
            }
            for (Gzmx_scdwVo bean : gzmx_scdwVoList) {
                if(bean.getJb() == 1){
                    recursionFn(gzmx_scdwVoList, bean);
                    resultList.add(bean);
                }
            }
            // 生产点位
            List<TreeSelect> treeSelectScdwList = resultList.stream().map(TreeSelect::new).collect(Collectors.toList());
            return treeSelectScdwList;
        }
        return list;
    }

    @Override
    public List<TreeSelect> selectGzmx_scdwBasicTreeSelectList(Gzmx_scdw gzmxScdw) {
        List<TreeSelect> list = new ArrayList<TreeSelect>();
        List<Gzmx_scdwVo> resultList = new ArrayList<Gzmx_scdwVo>();
        List<Gzmx_scdw> gzmxScdwList = this.selectGzmx_scdwList(gzmxScdw);
        if(gzmxScdwList != null){
            List<Gzmx_scdwVo> gzmx_scdwVoList = new ArrayList<Gzmx_scdwVo>(gzmxScdwList.size());
            Gzmx_scdwVo vo = null;
            for (Gzmx_scdw bean : gzmxScdwList) {
                vo = new Gzmx_scdwVo();
                BeanUtils.copyProperties(bean, vo);
                gzmx_scdwVoList.add(vo);
            }
            for (Gzmx_scdwVo bean : gzmx_scdwVoList) {
                if(bean.getJb() == 1){
                    recursionFn(gzmx_scdwVoList, bean);
                    resultList.add(bean);
                }
            }

            List<TreeSelect> treeSelectScdwList = resultList.stream().map(TreeSelect::new).collect(Collectors.toList());
            // 生产点位
            List<Gzmx_device> gzmx_deviceList = gzmxDeviceService.selectGzmx_deviceList(null);
            TreeSelect treeDevice = null;
            List<TreeSelect> childTreeDeviceList = null;
            List<TreeSelect> childTreeTagList = null;
            List<PointEquipment> pointEquipmentList = pointEquipmentService.selectPointEquipmentList(null);
            List<TreeSelect> resultTreeSelect = treeSelectScdwList.stream().peek(s -> {

                recursionFnTree(s, gzmx_deviceList, pointEquipmentList);
            }).collect(Collectors.toList());

            return treeSelectScdwList;
        }
        return list;
    }

    @Override
    public List<Gzmx_tag> reselectGzmx_reoscdwList(Gzmx_tag gzmx_tag) {
        return gzmx_scdwMapper.reoselectGzmx_scdwList(gzmx_tag);
    }

    private void recursionFnTree(TreeSelect treeSelect, List<Gzmx_device> gzmx_deviceList, List<PointEquipment> pointEquipmentList)
    {
        if(treeSelect.getChildren() != null){
            // 得到子节点列表
            for ( TreeSelect tree: treeSelect.getChildren()) {
                if("5".equals(tree.getLevel())){
                    ArrayList<TreeSelect> childTreeDeviceList = new ArrayList<TreeSelect>();
                    for (Gzmx_device device : gzmx_deviceList) {
                        if(judgeNode(device, tree)){
                            TreeSelect treeSelect1 = new TreeSelect(device);
                            // 采集点位子集
//                            ArrayList<TreeSelect>  childTreeTagList = new ArrayList<TreeSelect>();
//                            // 遍历设备点位关系表 查找与该设备绑定的采集点位
//                            for (PointEquipment pointEquipment : pointEquipmentList)  {
//                                // 上级 tree_id 为设备id
//                                // System.out.println("==>"+treeDevice.getId()+"==="+ pointEquipment.getEquipmentId() +"=="+(treeDevice.getId()).equals(pointEquipment.getEquipmentId()));
//                                if((treeSelect1.getId()).equals(pointEquipment.getEquipmentId())){
//                                    TreeSelect pointEquipmentTreeSelect = new TreeSelect(pointEquipment);
//                                    // 将添加进
//                                    childTreeTagList.add(pointEquipmentTreeSelect);
//                                }
//                            }
//                            treeSelect1.setChildren(childTreeTagList);
                            childTreeDeviceList.add(treeSelect1);
                        }
                    }
                    tree.setChildren(childTreeDeviceList);
                }
                recursionFnTree(tree , gzmx_deviceList, pointEquipmentList);
            }
        }
    }
    /**
     *  确定绑定关系，设备的第五级安装点位id与生产点位即treeselect ID相同
     * @param device
     * @param treeSelect
     * @return
     */
    private  boolean  judgeNode(Gzmx_device device, TreeSelect treeSelect){
        // 第五级生产点位为必填项
        if((device.getDev_azdw5_id()).equals(treeSelect.getId())){
            return true;
        }else {
            return  false;
        }
    }

    /**
     * 递归集合列表往数据里面添加children
     */
    private void recursionFn(List<Gzmx_scdwVo> list, Gzmx_scdwVo gzmxScdwVo)
    {
        // 得到子节点列表
        List<Gzmx_scdwVo> childList = getChildList(list, gzmxScdwVo);
        gzmxScdwVo.setChildren(childList);
        for (Gzmx_scdwVo tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<Gzmx_scdwVo> getChildList(List<Gzmx_scdwVo> list, Gzmx_scdwVo gzmxScdwVo)
    {
        List<Gzmx_scdwVo> tlist = new ArrayList<Gzmx_scdwVo>();
        Iterator<Gzmx_scdwVo> it = list.iterator();
        while (it.hasNext())
        {
            Gzmx_scdwVo n = (Gzmx_scdwVo) it.next();
            if (StringUtils.isNotNull(n.getFdwdm()) && n.getFdwdm().longValue() == gzmxScdwVo.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<Gzmx_scdwVo> list, Gzmx_scdwVo t)
    {
        return  getChildList(list, t).size() > 0;
    }

    /**
     * 模型管理左侧树 三级生产点位 + 场景
     * @return
     */
    public List<Ztree> temlateTreeData(){
        List<Ztree> ztreeList = new ArrayList<Ztree>();
        Integer maxId = gzmx_scdwMapper.selectScdwMaxId();
        List<Gzmx_scdw> gzmx_scdwList = gzmx_scdwMapper.selectGzmx_scdwList(new Gzmx_scdw());
        List<Gzmx_scdw> collect = gzmx_scdwList.stream().filter(scdw ->
                scdw.getJb() <= 3L
        ).collect(Collectors.toList());
        for (Gzmx_scdw scdw : collect) {
            Ztree ztree = new Ztree();
            ztree.setId(scdw.getId());
            ztree.setpId(scdw.getFdwdm());
            ztree.setName(scdw.getDwmc());
            ztree.setTitle(scdw.getDwmc());
            ztree.setLabel("scdw");
            ztreeList.add(ztree);
        }
        List<TSceneCategory> tSceneCategoryList = sceneCategoryMapper.selectTSceneCategoryList(null);
        for (TSceneCategory tSceneCategory : tSceneCategoryList) {
            Ztree ztree = new Ztree();
            ztree.setLabel("scene");
            if(tSceneCategory.getParentId() == 0){
                ztree.setId(convertId(maxId +"" +ZtreeContans.SCENE_PRE+ ""+tSceneCategory.getSceneId()));
                ztree.setName(tSceneCategory.getSceneName());
                ztree.setpId(tSceneCategory.getDw_3_id());
            }else{
                ztree.setId(convertId(maxId +"" +ZtreeContans.SCENE_PRE+ ""+tSceneCategory.getSceneId()));
                ztree.setName(tSceneCategory.getSceneName());
                ztree.setpId(convertId(maxId +"" +ZtreeContans.SCENE_PRE+ ""+tSceneCategory.getParentId()));
            }
            ztreeList.add(ztree);
        }
        return ztreeList;
    }



    private Long convertId(String str){
        return Long.parseLong(str);
    }

    /**
     * 基础管理左侧树 5级生产点位 + 设备
     * @return
     */
    public List<Ztree> jichuTreeData(){
        List<Ztree> ztreeList = new ArrayList<Ztree>();
        Integer maxId = gzmx_scdwMapper.selectScdwMaxId();
        List<Gzmx_scdw> gzmx_scdwList = gzmx_scdwMapper.selectGzmx_scdwList(new Gzmx_scdw());
        for (Gzmx_scdw scdw : gzmx_scdwList) {
            Ztree ztree = new Ztree();
            ztree.setId(scdw.getId());
            ztree.setpId(scdw.getFdwdm());
            ztree.setName(scdw.getDwmc());
            ztree.setTitle(scdw.getDwmc());
            ztree.setLabel("scdw");
            ztreeList.add(ztree);
        }

        List<Gzmx_device> gzmx_deviceList = gzmxDeviceService.selectGzmx_deviceList(null);
        for (Gzmx_device device : gzmx_deviceList) {
            Ztree ztree = new Ztree();
            ztree.setId(convertId(maxId+""+ ZtreeContans.DEVICE_PRE +""+ device.getId()));
            ztree.setpId(device.getDev_azdw5_id());
            ztree.setName(device.getDev_name());
            ztree.setTitle(device.getDev_name());
            ztree.setLabel("device");
            ztreeList.add(ztree);
        }
        return ztreeList;
    }


    /**
     *模型管理树
     * @return
     */
    @Override
    public List<ZtreeNew>  templateTreeDataNew(){
        List<ZtreeNew> ztreeNewList = new ArrayList<ZtreeNew>();
        Integer maxId = gzmx_scdwMapper.selectScdwMaxId();
        List<Gzmx_scdw> gzmx_scdwList = gzmx_scdwMapper.selectGzmx_scdwList(new Gzmx_scdw());
        List<Gzmx_scdw> collect = gzmx_scdwList.stream().filter(scdw ->
                scdw.getJb() <= 3L
        ).collect(Collectors.toList());
        for (Gzmx_scdw scdw : collect) {
            ZtreeNew ztreeNew = new ZtreeNew(scdw);
            ztreeNewList.add(ztreeNew);
        }

        List<TSceneCategory> tSceneCategoryList = sceneCategoryMapper.selectTSceneCategoryList(null);
        for (TSceneCategory tSceneCategory : tSceneCategoryList) {
            ZtreeNew ztreeNew = new ZtreeNew();
            ztreeNew.setLabel("scene");
            if(tSceneCategory.getParentId() == 0){
                ztreeNew.setId(convertId(maxId +"" +ZtreeContans.SCENE_PRE+ ""+tSceneCategory.getSceneId()));
                ztreeNew.setName(tSceneCategory.getSceneName());
                ztreeNew.setpId(tSceneCategory.getDw_3_id());
            }else{
                ztreeNew.setId(convertId(maxId +"" +ZtreeContans.SCENE_PRE+ ""+tSceneCategory.getSceneId()));
                ztreeNew.setName(tSceneCategory.getSceneName());
                ztreeNew.setpId(convertId(maxId +"" +ZtreeContans.SCENE_PRE+ ""+tSceneCategory.getParentId()));
            }
            ztreeNew.setEjdwId(tSceneCategory.getEjdwId());
            ztreeNew.setEjdwName(tSceneCategory.getEjdwName());
            ztreeNew.setDw_3_id(tSceneCategory.getDw_3_id());
            ztreeNew.setDw_3_name(tSceneCategory.getDw_3_name());
            ztreeNew.setDw_4_id(tSceneCategory.getDw_4_id());
            ztreeNew.setDw_4_name(tSceneCategory.getDw_4_name());
            ztreeNew.setDw_5_id(tSceneCategory.getDw_5_id());
            ztreeNew.setDw_5_name(tSceneCategory.getDw_5_name());
            ztreeNew.setTitle(tSceneCategory.getSceneName());
            ztreeNewList.add(ztreeNew);
        }
        return  ztreeNewList;
    }

    @Override
    public List<ZtreeNew> jichuTreeDataNew(){
        List<ZtreeNew> ztreeNewList = new ArrayList<ZtreeNew>();
        Integer maxId = gzmx_scdwMapper.selectScdwMaxId();
        List<Gzmx_scdw> gzmx_scdwList = gzmx_scdwMapper.selectGzmx_scdwList(new Gzmx_scdw());
        for (Gzmx_scdw scdw : gzmx_scdwList) {
            ZtreeNew ztreeNew = new ZtreeNew(scdw);
            ztreeNewList.add(ztreeNew);
        }

        List<Gzmx_device> gzmx_deviceList = gzmxDeviceService.selectGzmx_deviceList(null);
        for (Gzmx_device device : gzmx_deviceList) {
            ZtreeNew ztreeNew = new ZtreeNew();
            ztreeNew.setId(convertId(maxId + "" + ZtreeContans.DEVICE_PRE + "" + device.getId()));
            ztreeNew.setpId(device.getDev_azdw5_id());
            ztreeNew.setName(device.getDev_name());
            ztreeNew.setTitle(device.getDev_name());
            ztreeNew.setLabel("device");
            ztreeNewList.add(ztreeNew);
        }

        return ztreeNewList;
    }
}
